﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Configurations
{
    public class ConfigurationManager<T>
    {
        public List<T> Items { get; set; }
        public string XmlFile { get; set; }
        public  ConfigurationManager()
        {
 
        }
        public virtual T getItem(object o)//
        {

            return default(T);

        }
        public int getItemsCount()//
        {
            if (Items != null)
            {
                return Items.Count;
            }
            else
            {
                return 0;
            }
        }
        public void load()
        {
            if (File.Exists(XmlFile))
            {
                using (var fileStream = File.Open(XmlFile, System.IO.FileMode.Open))
                {
                    var xmlSerializtion = new XmlSerializer(typeof(List<T>));
                    this.Items = ((List<T>)xmlSerializtion.Deserialize(fileStream));
                }
            }
            else
            {
                this.Items = new List<T>();
            }
        }
        public bool Save()
        {
            try
            {
                using (var fileStream = File.Open(XmlFile, System.IO.FileMode.Create))
                {
                    var xmlSerializtion = new XmlSerializer(typeof(List<T>));
                    xmlSerializtion.Serialize(fileStream, Items);
                }
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }

    }
}
